package panda.client.ui.widgets;

import panda.client.Cardpanda;
import panda.client.engine.MagicCard;
import panda.client.enumerations.CardScale;
import panda.client.listeners.CardDragListener;
import panda.client.listeners.TooltipListener;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.Label;

/**
 * Draggable card Label with url and magic card information. Keeps track of attached mouselistener dragging state <.<
 * 
 * @author zane.tian@gmail.com
 */
public class DraggableCard extends Label implements InPlay {
	private MagicCard magicCard;
	private boolean dragging = false;
	private boolean tapped;

	public DraggableCard(MagicCard card) {
		super();

		this.magicCard = card;
		this.tapped = card.getTapped();

		setDimensionsAndBkgdImg();
		this.addMouseListener(new CardDragListener());
		this.addMouseListener(new TooltipListener());

		DOM.setStyleAttribute(getElement(), "zIndex", String.valueOf(card.getZIndex()));
	}

	public int getZIndex() {
		return magicCard.getZIndex();
	}

	public void setZIndex(int newIndex) {
		magicCard.setZIndex(newIndex);
		DOM.setStyleAttribute(getElement(), "zIndex", String.valueOf(newIndex));
	}

	public boolean isTapped() {
		return tapped;
	}

	public int getTapOffset() {
		int tapOffset = (CardScale.QUARTER.getHeight() - CardScale.QUARTER.getWidth()) / 2;
		return tapOffset;
	}

	public void tap() {
		magicCard.setTapped(true);
		tapped = true;

		DOM.setStyleAttribute(this.getElement(), "width", CardScale.QUARTER.getHeight() + "px");
		DOM.setStyleAttribute(this.getElement(), "height", CardScale.QUARTER.getWidth() + "px");
		DOM.setStyleAttribute(this.getElement(), "backgroundImage", "url(" + getTapUrl() + ")");

		int originalAbsX = this.getAbsoluteLeft() - Cardpanda.canvas.getAbsoluteLeft();
		int originalAbsY = this.getAbsoluteTop() - Cardpanda.canvas.getAbsoluteTop();

		Cardpanda.canvas.setWidgetPosition(this, originalAbsX - getTapOffset(), originalAbsY + getTapOffset());
	}

	public void untap() {
		magicCard.setTapped(false);
		tapped = false;
		setDimensionsAndBkgdImg();
		int originalAbsX = this.getAbsoluteLeft() - Cardpanda.canvas.getAbsoluteLeft();
		int originalAbsY = this.getAbsoluteTop() - Cardpanda.canvas.getAbsoluteTop();

		Cardpanda.canvas.setWidgetPosition(this, originalAbsX + getTapOffset(), originalAbsY - getTapOffset());
	}

	public void setDimensionsAndBkgdImg() {
		DOM.setStyleAttribute(this.getElement(), "width", CardScale.QUARTER.getWidth() + "px");
		DOM.setStyleAttribute(this.getElement(), "height", CardScale.QUARTER.getHeight() + "px");
		DOM.setStyleAttribute(this.getElement(), "backgroundImage", "url(" + getImgUrl() + ")");
	}

	public boolean isDragging() {
		return dragging;
	}

	public void setDragging(boolean dragging) {
		this.dragging = dragging;
	}

	public String getName() {
		return magicCard.getNameAndServerID();
	}

	public MagicCard getMagicCard() {
		return magicCard;
	}

	public long getServerID() {
		return getMagicCard().getServerID();
	}
	
	public String getTapUrl() {
		return magicCard.getTapUrl();
	}

	public String getImgUrl() {
		return magicCard.getImgUrl();
	}

	public String getZoomUrl() {
		return magicCard.getZoomUrl();
	}

	public String getUrl100() {
		return magicCard.getUrl100();
	}

	public String getUrl50() {
		return magicCard.getUrl50();
	}

	public String get180Url() {
		return magicCard.get180Url();
	}
}